package com.tzx.majj.tm.redis;

/**
 * @author majinding888@foxmail.com
 *  @date 2016-7-13 下午1:28:44
 */
import java.io.UnsupportedEncodingException;
import java.util.Scanner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;

public class Publisher {

	private static final Logger logger = LoggerFactory
			.getLogger(Publisher.class);

	private final Jedis publisherJedis;

	private final String channel;

	public Publisher(Jedis publisherJedis, String channel) {
		this.publisherJedis = publisherJedis;
		this.channel = channel;
	}

	public void start() {
		logger.info("Type your message (quit for terminate)");
		Scanner sc = new Scanner(System.in);

		while (true) {
			String line = sc.next();
			try {
				line = new String(line.getBytes("gbk"), "utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			if (!"quit".equals(line)) {
				publisherJedis.publish(channel, line);
			} else {
				break;
			}
		}

		sc.close();
	}
}